<?php
class Changtu8 {
	function get_city($city_py) {
		if (! $city_py)
			return array ();
		$db = Database::getInstance ();
		
		$sql = "SELECT city_id,city_pinyin,province_id,title from v9_ct_cities WHERE city_pinyin = ? LIMIT 1";
		$s = $db->queryOne ( $sql, $city_py );
		if (! $s)
			return array ();
		
		preg_match ( "/(.*)\,(.*)$/Us", $s ['title'], $arr );
		$s ['city_name'] = $arr [2];
		$s ['province_name'] = $arr [1];
		return $s;
	}
	function get_provinces() {
		$cachekey = md5 ( "get_provinces");
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		 
		$db = Database::getInstance ();
		//$sql = "select linkageid,name,description from 	v9_linkage where keyid = 3360 and (parentid = 0 or parentid = 3381) and linkageid != 3381";
		 $sql = "select linkageid,name,description from 	v9_linkage where keyid = 3360 and  parentid = 0 ";
		
		$prov = $db->query ( $sql );
		if ($cache) {
			$cache->set ( $cachekey, $prov, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		return $prov;
	}
	function get_links($city_id = 0,$count = 20){
		$cachekey = md5 ( "get_links_".$city_id."_".$count);
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		
		$db = Database::getInstance ();
		if ($city_id) { 
		//$sql = "select linkageid,name,description from 	v9_linkage where keyid = 3360 and (parentid = 0 or parentid = 3381) and linkageid != 3381";
			$sql = "SELECT title,url,city_id FROM `v9_links` WHERE city_id = ? limit ? ";
			$links = $db->query ( $sql,$city_id,$count );
		} 
		else { 
			$sql =   "SELECT title,url,city_id FROM `v9_links` limit ? ";
			$links = $db->query ( $sql ,$count);
		}
		if ($cache && $links) {
			$cache->set ( $cachekey, $links, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		return $links;
		
	}
	function get_news($city_id = 0,$count = 10){
		$cachekey = md5 ( "get_news_".$city_id."_".$count);
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
	
		$db = Database::getInstance ();
		if ($city_id) {
			//$sql = "select linkageid,name,description from 	v9_linkage where keyid = 3360 and (parentid = 0 or parentid = 3381) and linkageid != 3381";
			$sql = "SELECT id,title,keywords,province_id FROM `v9_news` WHERE province_id = ? and char_length(title)<=24 limit ? ";
			$news = $db->query ( $sql,$city_id,$count );
		}
		if (!isset($news) || empty($news))
		{
			$sql =   "SELECT id,title,keywords,province_id from `v9_news` where char_length(title)<=24 limit ? ";
			$news = $db->query ( $sql ,$count);
		}
		if ($cache && $news) {
			$cache->set ( $cachekey, $news, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		return $news;
	
	}
	
	function get_province($prov_id) {
		if (! $prov_id)
			return array ();
		$cachekey = md5 ( "get_province_" . $prov_id);
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		
		
		
		
		
		$db = Database::getInstance ();
		$sql = "select linkageid,name,description from 	v9_linkage where linkageid = ?";
		$prov = $db->queryOne($sql,$prov_id); 
		if ($cache) {
			$cache->set ( $cachekey, $prov, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		return $prov; 
	}
	/**
	 * city_list_get
	 * 
	 * @param unknown_type $p        	
	 * @return unknown
	 */
	function get_cities_by_prov($prov_id) {
		if (! $prov_id)
			return array ();

		$cachekey = md5 ( "get_cities_by_prov_" .$prov_id );
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
	
			 $db = Database::getInstance ();
		/*
		$sql = "select linkageid,name,description from 	v9_linkage where linkageid = ?";
		$prov = $db->queryOne($sql,$prov_id);  */
		
		$sql = "SELECT city_id,city_pinyin,province_id,title,upper(left(city_pinyin,1)) as first_char,substring_index(title,',',-1) as city_name from v9_ct_cities WHERE province_id = ?  order by city_pinyin asc";
		$cities = $db->query ( $sql, $prov_id );
		$result = array(); 
		foreach ( $cities as $s ) {
			if (!isset($result[$s['first_char']])) $result[$s['first_char']] = array(); 
			$result[$s['first_char']][] = $s;
		}
		if ($cache) {
			$cache->set ( $cachekey, $result, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
 	
		return $result;
	}
	
	/**
	 *
	 *
	 * station_list_get
	 *
	 * @param unknown_type $p
	 * @return unknown
	 */
	function get_stations_by_cityid($city_id) {
		if (! $city_id)
			return array ();
		$db = Database::getInstance ();
		$cachekey = md5 ( "get_stations_by_cityid_" . $city_id );
		$cache = Cache::getInstance ();
		if ($cache && $stations = $cache->get ( $cachekey ))
			return $stations; 
		
  
		$sql = "SELECT station_id,title,city_id,level,address,telphone,bus,description,thumb,keywords,updatetime from v9_ct_stations WHERE city_id = ?  order by line_count desc";
		
		$stations = $db->query($sql,$city_id);
		foreach ($stations as &$s) 
		{
			preg_match ( "/(.*)\,(.*)$/Us", $s ['title'], $arr );
			$s ['city_name'] = $arr [1];
			$s ['station_name'] = $arr [2];
				
			unset ( $arr );
			 
		}
		 
		if ($cache) {
			$cache->set ( $cachekey, $stations, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
	
		return $stations;
	}
	
	/**
	 * line_list_get_bysid
	 * 
	 * @param unknown_type $p        	
	 * @return unknown
	 */
	function get_lines_bysid($sid,$page = 1,$count = 9999) {
		
		
		if ($page<1) $page = 1; 
		
		$cachekey = md5 ( "get_lines_bysid_" . $sid . "_" .$page."_".$count );
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn; 
		
		$begin_item = ($page - 1) * $count;
		$limit_str = "limit $begin_item,$count";
		
		//$limit_str = "";
		$db = Database::getInstance();
		$results = array () ;
		$sql = "SELECT count(*) as cnt  from v9_ct_lines as l where l.station_id = ?"; 
		$count  = $db->queryOne($sql,$sid); 
		$results['cnt'] = $count['cnt'];
		$sql = "SELECT l.id,l.title,l.city_id,l.station_id,l.short_station_name,l.start_time,l.end_city_name,l.bus_type,l.distance,l.price,l.updatetime,c.city_pinyin from v9_ct_lines as l  left join v9_ct_cities as c on l.city_id = c.city_id  where l.station_id = ? order by l.end_city_name asc $limit_str";
		$lines  = $db->query($sql,$sid);  
		 
		$results['lines'] = $lines; 
		if ($cache) {
			$cache->set ( $cachekey, $results, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
	 	
		return $results;
	}
	function get_line($lid ,$city_py ) { 
		
		$cachekey = md5 ( "get_hot_stations_" . $lid . "_" .$city_py );
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		
		$db = Database::getInstance() ;
		 
		$sql = "SELECT vl.id,vl.title,vl.description,vl.city_id,vl.station_id ,vl.short_station_name,vl.start_time,vl.end_city_name,vl.bus_type,vl.distance,vl.price,vc.city_pinyin  from v9_ct_lines vl  left join v9_ct_cities as vc on vc.city_id = vl.city_id WHERE vl.id  = ? limit 1";
		
		$line = $db->queryOne($sql,$lid) ;
		if ($line) { 
			preg_match ( "/(.*?)-(.*?)\s*\((.*?),\d+:\d+\)$/Us", $line ['title'], $arr );
			$line ['city_name'] = $arr [1];
			$line ['station_name'] = $arr [3];
			
			 
		} 
	
		
		
		
		if ($cache) {
			$cache->set ( $cachekey, $line, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		return $line; 
	}
	/**
	 * station_get
	 * 
	 * @param unknown_type $p        	
	 * @return unknown multitype:
	 */
	function get_station($sid,$city_py = '' ) {
		$db = Database::getInstance(); 
		
		$cachekey = md5 ( "get_station" . $sid . "_" .$city_py );
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		
		$sql = "SELECT vs.station_id,vs.title,vs.city_id,vs.level,vs.address,vs.telphone,vs.bus,vs.description,vsdata.content,vs.thumb,vsdata.station_pictures,vs.keywords,vs.updatetime,vc.city_pinyin from v9_ct_stations vs left join v9_ct_stations_data vsdata on vs.id = vsdata.id left join v9_ct_cities as vc on vc.city_id = vs.city_id WHERE station_id  = ? and vc.city_pinyin = ? limit 1 ";
		
		$s = $db->queryOne($sql,$sid,$city_py); 
		if ($s) {
			preg_match ( "/(.*)\,(.*)$/Us", $s ['title'], $arr );
			$s ['city_name'] = $arr [1];
			$s ['station_name'] = $arr [2]; 
			$s ['short_station_name'] = str_replace($s ['city_name'], "", $s ['station_name']); 
		 
		}  
		if ($cache) {
			$cache->set ( $cachekey, $s, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		return $s; 
	}
	
	
	/**
	 * line_list_search
	 * 
	 * @param unknown_type $p        	
	 * @return multitype:unknown
	 */
	function search_lines_byfromto($from,$to,$page=1,$count=9999) {
		 
		$db = Database::getInstance();
		
		$cachekey = md5 ( "search_lines_byfromto_" . $from . "_" .$to );
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn; 
		
		$sql = "SELECT count(*) as cnt from v9_ct_lines where title like '" . $from . "-" . $to . "%' ";
		$rtn = array(); 
		$r  = $db->queryOne($sql);
		if ($r) 
			$rtn ['total'] =  $r['cnt']; 
		 
		$sql = "SELECT l.id,l.title,l.city_id,l.station_id,l.short_station_name,l.start_time,l.end_city_name,l.bus_type,l.distance,l.price,l.updatetime,c.city_pinyin from v9_ct_lines as l left join v9_ct_cities as  c on l.city_id = c.city_id where l.title like '" . $from . "-" . $to . "%' limit " . ($page - 1) * $count . "," . $count . "";
		// echo $sql;
		// exit;
		$results = $db->query($sql); 
		foreach ($results as &$s)
		{
			preg_match ( "/(.*)-(.*)$/Us", $s ['title'], $arr );
			$s ['city_name'] = $arr [1];
			$rtn ['city_pinyin'] = $s ['city_pinyin'];
			$rtn ['city_id'] = $s ['city_id'];		
			// $s['station_name'] = $arr[2];
			unset ( $arr );
			
		}
		$rtn ['lines'] = $results;
		if ($cache) {
			$cache->set ( $cachekey, $rtn, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		
		return $rtn;
	}
	/**
	 * 热门站站搜索
	 * c2c_hotlist_get
	 * 
	 * @param unknown_type $p        	
	 * @return unknown
	 */
	function get_hot_search($city_id = 0  ,$count=30) {
		

		$cachekey = md5 ( "get_hot_search_" . $city_id . "_" .$count );
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		
		if ($city_id)
			$where_str = " where l.city_id = $city_id "; 
		else 
			$where_str  = '' ; 
			 
		$db = Database::getInstance (); 
		
		$sql = "SELECT l.id,l.title,l.city_id,l.station_id,l.short_station_name,l.start_time,l.end_city_name,l.bus_type,l.distance,l.price,l.updatetime,c.city_pinyin from v9_ct_lines as l  left join v9_ct_cities as c on l.city_id = c.city_id $where_str  group by l.end_city_name order by l.listorder asc,id desc  limit $count";
		
		$results = $db->query($sql); 
		
		foreach ($results as &$s)
		{
			preg_match ( "/(.*)-(.*)$/Us", $s ['title'], $arr );
			$s ['city_name'] = $arr [1];
			// $s['station_name'] = $arr[2];
			unset ( $arr );
			 
		}

		if ($cache) {
			$cache->set ( $cachekey, $results, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		
		return $results;
	}
	 
	
	/**
	 * city_hotlist_get
	 * 
	 * @param unknown_type $p        	
	 * @return unknown
	 */
	function get_hot_cities($count = 30) {


		$cachekey = md5 ( "get_hot_cities_" .$count );
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		
		
		$db = Database::getInstance ();
		$sql = "SELECT city_id,city_pinyin,province_id,title,substring_index(title,',',1) province_name,substring_index(title,',',-1) city_name from v9_ct_cities 
where char_length(substring_index(title,',',-1)) <=3 order by line_count desc ,listorder desc limit ?  ";
		
		$results = $db->query($sql,$count);
		
		/* foreach ($results as &$s)
		{
			
			preg_match ( "/(.*)\,(.*)$/Us", $s ['title'], $arr );
			
			$s ['city_name'] = $arr [2];
			$s ['province_name'] = $arr [1];
			if (mb_strlen($s['city_name'],'UTF-8' ) > 3) unset($s); 
			unset ( $arr );
			
		} */
		
		if ($cache) {
			$cache->set ( $cachekey, $results, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		
		return $results;
	}
	/**
	 * station_hotlist_get
	 * 
	 * @param unknown_type $p        	
	 * @return unknown
	 */
	function get_hot_stations($count = 30) {
		
		
		$cachekey = md5 ( "get_hot_stations_".$count);
		$cache = Cache::getInstance ();
		if ($cache && $rtn = $cache->get ( $cachekey ))
			return $rtn;
		
		$db = Database::getInstance ();
		$sql = "SELECT s.station_id,s.title,s.city_id,s.level,s.address,s.telphone,s.bus,c.city_pinyin from v9_ct_stations as s left join v9_ct_cities as c on s.city_id = c.city_id order by s.listorder limit ?  ";
		
		$results = $db->query($sql,$count);
		
		foreach ($results as &$s)
		{
			preg_match ( "/(.*)\,(.*)$/Us", $s ['title'], $arr );
			$s ['station_name'] = $arr [2];
			$s ['city_name'] = $arr [1];
			unset ( $arr );
				
		}

		if ($cache) {
			$cache->set ( $cachekey, $results, 3600 * 24 * 7, MEMCACHE_COMPRESSED );
		}
		
		return $results; 
		
		 
	}
	
	 
	
	/*
	 * static function api_open($param) { $url =
	 * Context::getConfig("ct8_api_url"); $param_str =
	 * urlencode(json_encode($param)); $cache_key = "ct8_data_" .
	 * md5($param_str); $cache = Cache::getInstance(); if ($cache && $result =
	 * $cache->get($cache_key)) { return $result; }
	 * //echo($url."?cmd=".$param_str); $result = file_get_contents($url .
	 * "?cmd=" . $param_str); //var_export( $result) ; $result =
	 * get_object_vars(json_decode($result)); //foreach($result as $k=>$v)
	 * $result[$k] = get_object_vars($v); if ($cache) $cache->set($cache_key,
	 * $result, 10, MEMCACHE_COMPRESSED); //var_export( $result) ; return
	 * $result; }
	 */
}

?>
